package org.fuxia.im.listeners;

import java.awt.Component;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.fuxia.im.common.ClientConst;
import org.fuxia.im.common.ClientContext;
import org.fuxia.im.core.entries.ECResponse;
import org.fuxia.im.core.listeners.Listener;
import org.fuxia.im.gui.AddFriend;
import org.fuxia.im.gui.Main;

public class AddUserGroupListener implements Listener{

	public void trigger(Object param) {
		ECResponse resp = (ECResponse)param ;
		Component parent = null ;
		Object currentResp = ClientContext.removeKey(ClientConst.CURRENT_REQUESTOR) ;
		if(currentResp != null) {
			parent = (Component)currentResp ;
		}
		if (resp.getStatusCode() == null || resp.getStatusCode() != 200)
		{
			JOptionPane.showMessageDialog(null, resp.getResponseText()) ;
			return;
		}else{
			handlerSuccess(resp , parent) ;
		}
	}
	
	public void handlerSuccess(final ECResponse resp , Component current){
		final AddFriend add = (AddFriend)current ;
		final Main main = (Main) ClientContext.getValue(ClientConst.MAIN_WINDOW) ;
		SwingUtilities.invokeLater(new Runnable() {
			
			public void run() {
				String groupId = resp.get("groupId").toString() ;
				String groupName = resp.get("groupName").toString() ;
				add.addInfo(groupName, groupId) ;
				main.addGroup(groupId, groupName) ;
			}
		});
	}

}
